home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2005 June
/
ccd0605.iso
/
Software
/
Freeware
/
Programare
/
highlight
/
highlight-W32GUI-2.2-10b-Setup.exe
/
{app}
/
src
/
htmlgenerator.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2005-03-31
|
9KB
|
293 lines
/***************************************************************************
htmlcode.cpp - description
-------------------
begin : Wed Nov 28 2001
copyright : (C) 2001 by AndrΘ Simon
email : andre.simon1@gmx.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "htmlgenerator.h"
using namespace std;
namespace highlight {
HtmlGenerator::HtmlGenerator(void)
{}
string HtmlGenerator::formatStyleAttributes(const string & elemName,
const ElementStyle & elem)
{
ostringstream s;
s << "."<<elemName<<"\t{ color:#"
<< (elem.getColour().getHexRedValue())
<< (elem.getColour().getHexGreenValue())
<< (elem.getColour().getHexBlueValue() )
<< ( elem.isBold() ?"; font-weight:bold" :"" )
<< ( elem.isItalic() ?"; font-style:italic" :"" )
<< ( elem.isUnderline() ?"; text-decoration:underline" :"" )
<< "; }\n" ;
return s.str();
}
string HtmlGenerator::getOpenTag(const string& styleName ){
return "<span class=\""+styleName+"\">";
}
HtmlGenerator::HtmlGenerator (
const string &cssStyle,
const string &enc,
bool omitEnc,
bool withAnchors)
: CodeGenerator( cssStyle),
brTag("<br>"),
hrTag("<hr>"),
idAttr("name"),
fileSuffix(".html"),
encoding(enc),
omitEncoding(omitEnc),
HTML_FOOTER(
"\n</body>\n</html>\n<!--HTML generated by highlight "
HIGHLIGHT_VERSION
", "
HIGHLIGHT_URL
"-->\n"),
attachAnchors(withAnchors)
{
styleTagOpen.push_back("");
styleTagOpen.push_back(getOpenTag("str"));
styleTagOpen.push_back(getOpenTag("num"));
styleTagOpen.push_back(getOpenTag("slc"));
styleTagOpen.push_back(getOpenTag("com"));
styleTagOpen.push_back(getOpenTag("esc"));
styleTagOpen.push_back(getOpenTag("dir"));
styleTagOpen.push_back(getOpenTag("dstr"));
styleTagOpen.push_back(getOpenTag("line"));
styleTagOpen.push_back(getOpenTag("sym"));
styleTagClose.push_back("");
for (int i=1;i<NUMBER_BUILTIN_STYLES; i++) {
styleTagClose.push_back("</span>");
}
/*assert (styleTagOpen.size()==styleTagClose.size());
assert (styleTagOpen.size()==NUMBER_BUILTIN_STYLES);
*/
newLineTag = "\n";
spacer = " ";
styleCommentOpen="/*";
styleCommentClose="*/";
}
string HtmlGenerator::getStyleDefinition()
{
if (styleDefinitionCache.empty()){
ostringstream os;
os << "body.hl\t{ background-color:#"
<< (docStyle.getBgColour().getHexRedValue())
<< (docStyle.getBgColour().getHexGreenValue())
<< (docStyle.getBgColour().getHexBlueValue())
<< "; }\n";
os << "pre.hl\t{ color:#"
<< (docStyle.getDefaultStyle().getColour().getHexRedValue())
<< (docStyle.getDefaultStyle().getColour().getHexGreenValue())
<< (docStyle.getDefaultStyle().getColour().getHexBlueValue() )
<< "; background-color:#"
<< (docStyle.getBgColour().getHexRedValue())
<< (docStyle.getBgColour().getHexGreenValue())
<< (docStyle.getBgColour().getHexBlueValue())
<< "; font-size:"
<< docStyle.getFontSize()
<< "pt; font-family:Courier;}\n";
os << formatStyleAttributes("num", docStyle.getNumberStyle())
<< formatStyleAttributes("esc", docStyle.getEscapeCharStyle())
<< formatStyleAttributes("str", docStyle.getStringStyle())
<< formatStyleAttributes("dstr", docStyle.getDirectiveStringStyle())
<< formatStyleAttributes("slc", docStyle.getSingleLineCommentStyle())
<< formatStyleAttributes("com", docStyle.getCommentStyle())
<< formatStyleAttributes("dir", docStyle.getDirectiveStyle())
<< formatStyleAttributes("sym", docStyle.getSymbolStyle())
<< formatStyleAttributes("line", docStyle.getLineStyle());
KeywordStyles styles = docStyle.getKeywordStyles();
for (KSIterator it=styles.begin(); it!=styles.end(); it++){
os << formatStyleAttributes(it->first, *(it->second));
}
styleDefinitionCache=os.str();
}
return styleDefinitionCache;
}
string HtmlGenerator::getHeader(const string &title)
{
ostringstream os;
os << getHeaderStart((title.empty())?"Source file":title );
if (langInfo.getSyntaxHighlight())
{
if (includeStyleDef) //CSS-Definition in HTML-<head> einfuegen
{
os << "<style type=\"text/css\">\n";
os << "<!--\n";
os << getStyleDefinition();
os << CodeGenerator::readUserStyleDef();
os << "//-->\n";
os << "</style>" << endl;
}
else //Referenz auf CSS-Datei einfuegen
{
os << "<link rel=\"stylesheet\" type=\"text/css\" href=\""
<< getStyleOutputPath()
<< "\""
<< ">\n";
}
}
os << "</head>\n<body class=\"hl\">\n<pre class=\"hl\">";
return os.str();
}
string HtmlGenerator::getFooter()
{
return "</pre>" + HTML_FOOTER;
}
void HtmlGenerator::printBody()
{
processRootState();
}
string HtmlGenerator::maskCharacter(unsigned char c)
{
switch (c) {
case '<' :
return "<";
break;
case '>' :
return ">";
break;
case '&' :
return "&";
break;
case '\"' :
return """;
break;
case '@' :
return "@";
break;
default :
string m;
return m += c;
}
}
void HtmlGenerator::insertLineNumber (bool insertNewLine)
{
if (insertNewLine){
//*out << getNewLine();
wsBuffer += getNewLine();
}
if (showLineNumbers) {
ostringstream numberPrefix;
if (attachAnchors) {
numberPrefix << "<a "
<< idAttr
<< "=\"l_"
<< lineNumber
<< "\">";
}
ostringstream os;
if (lineNumberFillZeroes) os.fill('0');
os <<setw(LINE_NUMBER_WIDTH)<<right<< lineNumber;
numberPrefix<< styleTagOpen[LINENUMBER]
<< os.str()
<< spacer
<< styleTagClose[LINENUMBER];
if (attachAnchors) {
numberPrefix << "</a>";
}
wsBuffer += numberPrefix.str();
}
}
string HtmlGenerator::getHeaderStart(const string &title){
ostringstream header;
header<< "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"
<< "\n<html>\n<head>\n";
if (!omitEncoding){
header << "<meta http-equiv=\"content-type\" content=\"text/html; charset="<<encoding<<"\">\n";
}
header << "<title>" << title <<"</title>\n";
return header.str();
}
bool HtmlGenerator::printIndexFile(const vector<string> &fileList,
const string &outPath ){
string suffix = fileSuffix;
string outFilePath = outPath + "index" + suffix;
ofstream indexfile(outFilePath.c_str());
if (!indexfile.fail()){
string inFileName;
string inFilePath, newInFilePath;
indexfile << getHeaderStart("Source Index" );
indexfile << "</head>\n<body>\n<h1> Source Index</h1>\n"
<< hrTag
<< "\n<ul>\n";
string::size_type pos;
for (unsigned int i=0; i < fileList.size(); i++){
pos=(fileList[i]).find_last_of(Platform::pathSeparator);
if (pos!=string::npos){
newInFilePath = (fileList[i]).substr(0, pos+1);
} else {
newInFilePath=Platform::pathSeparator;
}
if (newInFilePath!=inFilePath){
indexfile << "</ul>\n<h2>";
indexfile << newInFilePath;
indexfile << "</h2>\n<ul>\n";
inFilePath=newInFilePath;
}
inFileName = (fileList[i]).substr(pos+1);
indexfile << "<li><a href=\"" << inFileName << suffix << "\">";
indexfile << inFileName << suffix <<"</a></li>\n";
}
indexfile << "</ul>\n"
<< hrTag << brTag
<< "<small>Generated by highlight "
<< HIGHLIGHT_VERSION
<< ", <a href=\"" << HIGHLIGHT_URL << "\" target=\"new\">"
<< HIGHLIGHT_URL << "</a></small>";
indexfile << HTML_FOOTER;
} else {
return false;
}
return true;
}
string HtmlGenerator::getMatchingOpenTag(unsigned int styleID){
return getOpenTag(langInfo.getKeywordClasses()[styleID]);
}
string HtmlGenerator::getMatchingCloseTag(unsigned int styleID){
return "</span>";
}
}